//
//  ZXLUnit.m
//  ZhiXingLiPlus
//
//  Created by Apple on 2018/1/31.
//  Copyright © 2018年 wwtech. All rights reserved.
//

#import "ZXLUnit.h"

@implementation ZXLUnit

/****----文件处理----***/
/**
 *  保存文件
 *
 *  @param name 文件名
 *  @param theData 文件数
 *
 *  @return 如果有就设置，然后返回yes；如果没有就返回no
 */

+ (NSString *)saveFileWithHome:(NSString *)home
                      fileName:(NSString *)name
                      withData:(NSData *)theData {
    
    NSFileManager *fm = [NSFileManager defaultManager];
    //判断文件夹是否存在如果不存在就创建
    if (![fm fileExistsAtPath:home]) {
        [fm createDirectoryAtPath:home withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    NSDictionary *fileDic = [[self class] legalFileNameWithName:name home:home fileManager:fm];
    NSString *file = fileDic[@"file"];//文件路径
    if([theData writeToFile:file atomically:YES]) {
        return fileDic[@"name"];//最终文件名
    } else {
        return nil;
    }
}

/**
 *  获取合法文件名
 *
 *  @param name 文件名
 *  @param home 目录
 *  @param fm fileManager
 *
 *  @return 如果有就设置，然后返回yes；如果没有就返回no
 */

+ (NSDictionary *)legalFileNameWithName:(NSString *)name
                                   home:(NSString *)home
                            fileManager:(NSFileManager *)fm {
    
    NSString *file = [home stringByAppendingPathComponent:name];
    
    if ([fm fileExistsAtPath:file]) {
        
        NSArray *classArray = [name componentsSeparatedByString:@"."];
        NSString *fileClass = [classArray lastObject];
        NSString *fileName = [classArray firstObject];
        
        NSArray *array = [fileName componentsSeparatedByString:@"_"];
        
        fileName = @"";
        if (array.count > 1 && [[array lastObject] intValue] > 0) {
            for (int i = 0; i < array.count - 1; i ++) {
                if (i > 0) {
                    fileName = [fileName stringByAppendingString:[NSString stringWithFormat:@"_%@",[array objectAtIndex:i]]];
                } else {
                    fileName = [array objectAtIndex:0];
                }
            }
            name = [fileName stringByAppendingString:[NSString stringWithFormat:@"_%d.%@", [[array lastObject] intValue] + 1,fileClass]];
        } else {
            name = [fileName stringByAppendingString:[NSString stringWithFormat:@"%@_1.%@", [array firstObject], fileClass]];
        }
        //此处用的递归
        return [[self class] legalFileNameWithName:name home:home fileManager:fm];
    } else {
        return @{
                 @"file": file,
                 @"name": name
                 };
    }
}

+ (UIImage *)compressImgWithSourceImg:(UIImage *)sourceImg
                          TargetWidth:(CGFloat)targetWidth {
    
    // 源图片的分辨率
    CGSize sourceImgSize = sourceImg.size;
    CGFloat sourceImgWidth = sourceImgSize.width;
    CGFloat sourceImgHeight = sourceImgSize.height;
    
    // 压缩图片的分辨率
    CGFloat compressImgWidth = targetWidth;
    CGFloat compressImgHeight = (targetWidth / sourceImgWidth) * sourceImgHeight;
    
    // 压缩图片
    // 把预设的图片分辨率设置为当前图片上下文
    UIGraphicsBeginImageContext(CGSizeMake(compressImgWidth, compressImgHeight));
    // drawInRect 改变图片的像素
    [sourceImg drawInRect:CGRectMake(0, 0, compressImgWidth, compressImgHeight)];
    // 从当前图片上下文中创建新图片
    UIImage *compressImg = UIGraphicsGetImageFromCurrentImageContext();
    // 使当前图片上下文出堆栈
    UIGraphicsEndImageContext();
    
    return compressImg;
}

+ (void)drawDashLine:(UIView *)lineView isVertical:(BOOL)isVertical lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor {
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setBounds:lineView.bounds];
    [shapeLayer setPosition:CGPointMake(isVertical ? CGRectGetWidth(lineView.frame) : CGRectGetWidth(lineView.frame) / 2, isVertical ? CGRectGetHeight(lineView.frame) / 2 : CGRectGetHeight(lineView.frame))];
    [shapeLayer setFillColor:[UIColor clearColor].CGColor];
    // 设置虚线颜色
    [shapeLayer setStrokeColor:lineColor.CGColor];
    // 设置虚线宽度
    [shapeLayer setLineWidth:isVertical ? CGRectGetWidth(lineView.frame) : CGRectGetHeight(lineView.frame)];
    [shapeLayer setLineJoin:kCALineJoinRound];
    // 设置线宽,线间距
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];
    // 设置路径
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 0, 0);
    CGPathAddLineToPoint(path, NULL, isVertical ? 0 : CGRectGetWidth(lineView.frame), isVertical ? CGRectGetHeight(lineView.frame) : 0);
    [shapeLayer setPath:path];
    CGPathRelease(path);
    // 把绘制好的虚线添加上来
    [lineView.layer addSublayer:shapeLayer];
}

@end
